package com.net

import androidx.annotation.Keep
import okhttp3.CookieJar
import okhttp3.Interceptor

/**
 * @describe: 网络客户端配置项，可以扩展
 * @Author: lixiaopeng
 * @Date: 2020/6/24
 */
@Keep
class NetClientConfig private constructor(val readTimeOut: Long,
                                          val connectTimeout: Long,
                                          val writeTimeout: Long,
                                          val cookieJar: CookieJar?,
                                          val interceptors: MutableList<Interceptor>,
                                          val netInterceptors: MutableList<Interceptor>,
                                          val tag: String?,
                                          val debug: Boolean) {

    class Builder : INetClientConfig {
        private var readTimeOut: Long = 5000
        private var connectTimeout: Long = 5000
        private var writeTimeout: Long = 5000
        private var cookieJar: CookieJar? = null
        private var tag: String? = null
        private var debug = false
        private val interceptors: MutableList<Interceptor> = mutableListOf()
        private val netInterceptors: MutableList<Interceptor> = mutableListOf()

        fun build(): NetClientConfig {
            return NetClientConfig(readTimeOut, connectTimeout, writeTimeout, cookieJar, interceptors, netInterceptors, tag, debug)
        }


        override fun setReadTimeOut(readTimeOut: Long): Builder {
            this.readTimeOut = readTimeOut
            return this
        }

        override fun setConnectTimeout(connectTimeOut: Long): Builder {
            connectTimeout = connectTimeOut
            return this
        }

        override fun setWriteTimeout(writeTimeOut: Long): Builder {
            writeTimeout = writeTimeOut
            return this
        }

        override fun setCookieJar(cookieJar: CookieJar?): Builder {
            this.cookieJar = cookieJar
            return this
        }

        override fun debug(debug: Boolean): Builder {
            this.debug = debug
            return this
        }

        override fun addInterceptor(interceptor: Interceptor?): Builder {
            interceptor?.let {
                interceptors.add(it)
            }
            return this
        }

        override fun addNetInterceptor(interceptor: Interceptor?): Builder {
            interceptor?.let {
                netInterceptors.add(it)
            }
            return this
        }

        override fun setTag(tag: String) : Builder{
            this.tag = tag
            return this
        }
    }

    interface INetClientConfig {
        fun setReadTimeOut(readTimeOut: Long): Builder
        fun setConnectTimeout(connectTimeOut: Long): Builder
        fun setWriteTimeout(writeTimeOut: Long): Builder
        fun setCookieJar(cookieJar: CookieJar?): Builder
        fun debug(debug: Boolean): Builder
        fun addInterceptor(interceptor: Interceptor?): Builder
        fun addNetInterceptor(interceptor: Interceptor?): Builder
        fun setTag(tag: String): Builder
    }
}